#include "stdafx.h"
#include "CSheep.h"
#include <iostream>
using namespace std;

CSheep::CSheep()
{
}


CSheep::~CSheep()
{
}

void CSheep::LoadImag()
{
	m_sheepimg = cvLoadImage("./picture/sheep0.jpg");
}

void CSheep::DrawtoBk(IplImage * pbk)
{
	if (pbk != NULL && m_sheepimg != NULL)
	{
		for (int i = 0; i < m_sheepimg->height; i++)
		{
			for (int j = 0; j < m_sheepimg->width; j++)
			{
				int bch = CV_IMAGE_ELEM(m_sheepimg, uchar, i, j * 3 + 0);
				int gch = CV_IMAGE_ELEM(m_sheepimg, uchar, i, j * 3 + 1);
				int rch = CV_IMAGE_ELEM(m_sheepimg, uchar, i, j * 3 + 2);
				if (!(gch > 240))
				{
					CV_IMAGE_ELEM(pbk, uchar, m_pos.y*60 + i, (j + m_pos.x*60) * 3 + 0) = CV_IMAGE_ELEM(m_sheepimg, uchar, i, j * 3 + 0);
					CV_IMAGE_ELEM(pbk, uchar, m_pos.y*60 + i, (j + m_pos.x*60) * 3 + 1) = CV_IMAGE_ELEM(m_sheepimg, uchar, i, j * 3 + 1);
					CV_IMAGE_ELEM(pbk, uchar, m_pos.y*60 + i, (j + m_pos.x*60) * 3 + 2) = CV_IMAGE_ELEM(m_sheepimg, uchar, i, j * 3 + 2);
				}
			}
		}
	}
	else if (pbk == NULL)
		cout << "pbk is null";
	else if (m_sheepimg==NULL)
		cout << "sheep is null";
}

int CSheep::BloodReduce()
{
	return m_blood = 0;
}

void CSheep::ShowAnimal()
{
	cout << "This is a sheep" << endl;
	if (m_gender == 0)
		cout << "The sheep is male. It is " << m_age << " years old."
		<< "Its blood level is " << m_blood <<". Its location is("<<m_pos.x<<","<<m_pos.y<<")" << endl;
	else
		cout << "The sheep is female. It is " << m_age << " years old."
		<< "Its blood level is " << m_blood  <<". Its location is(" << m_pos.x << "," << m_pos.y << ")" << endl;
}

void CSheep::Print(int flag, int i)
{
	fstream file("./results.txt", ios::app);
	if (file.is_open())
	{
		if (flag == 0)
		{
			cout << "Sheep " << (i + 1) << " was dead!" << endl;
			file << "Sheep " << (i + 1) << " was dead!" << endl;
		}
		else
		{
			cout << "Sheep " << (i + 1) << " is alive!" << endl;
			file << "Sheep " << (i + 1) << " is alive!" << endl;
		}
	}
	file.close();
}

void CSheep::EatGrass(CGrass *pgrass, IplImage *pbk)
{
	m_distance = sqrt((m_pos.x * 60 - pgrass->m_pos.x * 60)*(m_pos.x * 60 - pgrass->m_pos.x * 60) + (m_pos.y * 60 - pgrass->m_pos.y * 60)*(m_pos.y * 60 - pgrass->m_pos.y * 60));
	if (m_distance <= 60)
	{
		pgrass->DrawGrassDie(pbk);
		pgrass->ReleaseImg();
	}
}

void CSheep::Diedraw(IplImage * pbk)
{
	m_sheepdieimg = cvLoadImage("./picture/sheepdie.jpg");
	if (pbk != NULL && m_sheepdieimg != NULL)
	{
		for (int i = 0; i < m_sheepdieimg->height; i++)
		{
			for (int j = 0; j < m_sheepdieimg->width; j++)
			{
				int bch = CV_IMAGE_ELEM(m_sheepdieimg, uchar, i, j * 3 + 0);
				int gch = CV_IMAGE_ELEM(m_sheepdieimg, uchar, i, j * 3 + 1);
				int rch = CV_IMAGE_ELEM(m_sheepdieimg, uchar, i, j * 3 + 2);
				//if (!(bch > 245 && gch > 245 && rch > 245))
				//{
					CV_IMAGE_ELEM(pbk, uchar, m_pos.x * 60 + i, (j + m_pos.y * 60) * 3 + 0) = CV_IMAGE_ELEM(m_sheepdieimg, uchar, i, j * 3 + 0);
					CV_IMAGE_ELEM(pbk, uchar, m_pos.x * 60 + i, (j + m_pos.y * 60) * 3 + 1) = CV_IMAGE_ELEM(m_sheepdieimg, uchar, i, j * 3 + 1);
					CV_IMAGE_ELEM(pbk, uchar, m_pos.x * 60 + i, (j + m_pos.y * 60) * 3 + 2) = CV_IMAGE_ELEM(m_sheepdieimg, uchar, i, j * 3 + 2);
				//}
			}
		}
	}
}

